<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="省份" prop="province">
          <el-select
            v-model="queryParams.province"
            placeholder="全部"
            clearable
            class="!w-[100px]"
          >
            <el-option value="1" label="视频" />
            <el-option value="2" label="音乐" />
            <el-option value="3" label="动漫" />
            <el-option value="4" label="游戏" />
            <el-option value="5" label="电信" />
          </el-select>
        </el-form-item>
        <el-form-item prop="bizName" label="业务名称">
          <el-input
            v-model="queryParams.bizName"
            placeholder="业务名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="业务价格">
          <el-input
            v-model="queryParams.bizPrice"
            placeholder="业务价格"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="业务id">
          <el-input
            v-model="queryParams.bizID"
            placeholder="业务id"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="渠道号">
          <el-input
            v-model="queryParams.bizNo"
            placeholder="渠道号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="业务类型">
          <el-input
            v-model="queryParams.bizType"
            placeholder="业务类型"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchTime"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="conversionRateList"
        highlight-current-row
        border
      >
        <el-table-column label="业务名称" width="110" prop="bizName" sortable />
        <el-table-column label="业务价格" width="100" prop="bizPrice" />
        <el-table-column label="业务id" width="100" prop="bizID" />
        <el-table-column label="业务类型" width="110" prop="bizType" sortable />
        <el-table-column label="渠道号" width="100" prop="bizNo" sortable />
        <el-table-column
          label="计费成功数"
          align="center"
          prop="success"
          width="120"
          sortable
        />
        <el-table-column
          label="计费请求数"
          prop="request"
          width="120"
          sortable
        />
        <el-table-column
          label="计费用户数"
          align="center"
          prop="user"
          width="120"
          sortable
        />
        <el-table-column
          label="请求转化率"
          align="center"
          prop="requestTotal"
          width="120"
          sortable
        >
          <template #default="scope">
            {{ convertToRate(scope.row.requestTotal) }}%
          </template>
        </el-table-column>
        <el-table-column
          label="用户转化率"
          align="center"
          prop="userTotal"
          width="120"
          sortable
        >
          <template #default="scope">
            {{ convertToRate(scope.row.userTotal) }}%
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { getTotalPage } from "@/api/total";
import { TotalQuery, TotalPageVO } from "@/api/total/types";
import { convertToRate } from "@/utils/index";

defineOptions({
  name: "Role",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);
const searchTime = ref(null);
const queryParams = reactive<TotalQuery>({
  page: 1,
  size: 10,
});

const conversionRateList = ref<TotalPageVO[]>();

function initSearchTime() {
  // const end = new Date();
  // const start = new Date();
  // start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);

  const start = new Date(new Date().getTime() - 3600 * 1000 * 24 * 7);
  const end = new Date(
    `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 23:59:59`
  );
  searchTime.value = [start, end];
  queryParams.dateStart = start.toISOString();
  queryParams.dateEnd = end.toISOString();
}
/** 查询 */
function handleQuery() {
  loading.value = true;
  getTotalPage(queryParams)
    .then((data) => {
      console.log("🚀 ~ .then ~ data:", data);
      conversionRateList.value = data.rows;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

onMounted(() => {
  initSearchTime();
  handleQuery();
});
</script>
